﻿using DataLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Common;

namespace TicketSystem
{
    public partial class Form_Login : Form
    {
        public Form_Login()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        PublicMethod pm = new PublicMethod();
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void btn_Login_Click(object sender, EventArgs e)
        {            
            int c = tse.Operators.Count();
            if (c < 1)
            {
                MessageBox.Show("尚无任何操作员");
            }
            else 
            {
                var password = pm.EncryptString(txt_Password.Text);
                var users = tse.Operators.Where(u => u.Name == (string)cbox_User.SelectedValue && u.Password == password).ToList();
                if (users.Count() == 1)
                {
                    var user = users.First();
                    var price = tse.TicketTypes.ToList();
                    this.DialogResult = DialogResult.OK;
                    GetOperator.OperatorID = user.OperatorID;
                    GetOperator.RoleID = user.RoleID;
                    GetOperator.OperatorName = user.Name;
                    Price.PriceOne = Math.Round(price.Find(u => u.TypeID == 1).Price,2);
                    Price.PriceTwo = Math.Round(price.Find(u => u.TypeID == 2).Price, 2);
                    Price.PriceThree = Math.Round(price.Find(u => u.TypeID == 3).Price, 2);
                    Price.PriceParkAdult = Math.Round(price.Find(u => u.TypeID == 4).Price, 2);
                    Price.PriceParkSpecial = Math.Round(price.Find(u => u.TypeID == 4).SpecialPrice, 2);
                    users.First().Logined_at = System.DateTime.Now;
                    tse.SaveChanges();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
            }
        }

        private void Press_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//如果输入的是回车键  
            {
                this.btn_Login_Click(sender, e);//触发button事件  
            }  
        }

        private void Login_Load(object sender, EventArgs e)
        {            
            var users = tse.Operators;
            List<string> list = new List<string>();
            foreach (var user in users)
            {
                list.Add(user.Name);
            }
            cbox_User.DataSource = list;
            var source = new AutoCompleteStringCollection();
            foreach (var id in list)
            {
                source.Add(id);
            }
            cbox_User.AutoCompleteCustomSource = source;
            cbox_User.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cbox_User.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }
    }
}
